package com.qqtech.qquan.send.controller;

import java.math.BigDecimal;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort.Direction;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.qqtech.admin.context.AdminPage;
import com.qqtech.core.common.util.StringUtil;
import com.qqtech.core.common.util.TimeUtil;
import com.qqtech.core.frame.model.PageKit;
import com.qqtech.core.frame.model.PageOrder;
import com.qqtech.core.frame.model.Result;
import com.qqtech.core.frame.service.BaseService;
import com.qqtech.qquan.homemaking.model.HomemakingPubitem;
import com.qqtech.qquan.homemaking.model.HomemakingPubitemVo;
import com.qqtech.qquan.homemaking.model.HomemakingTypeVo;
import com.qqtech.qquan.send.model.SendDeliverySysParam;
import com.qqtech.qquan.send.model.SendDeliverySysParamQuery;
import com.qqtech.qquan.send.model.SendDeliverySysParamVo;
import com.qqtech.qquan.send.service.SendDeliverySysParamService;
import com.qqtech.qquan.sys.controller.BaseAdminController;

@Controller
@RequestMapping(value = "/send/deliverySysParam")
public class SendDeliverySysParamController extends BaseAdminController {
	@Resource
	private SendDeliverySysParamService sendDeliverySysParamService;
	
	@Override
	protected BaseService<SendDeliverySysParam> getBaseService() {
		return sendDeliverySysParamService;
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.GET)
	public String list(Model model) {
		return "/send/deliverySysParam-list";
	}
	
	@RequestMapping(value = "/list", method = RequestMethod.POST)
	@ResponseBody
	public AdminPage list(SendDeliverySysParamQuery query,
			HttpServletRequest request, HttpServletResponse response) {
		AdminPage adminPage = new AdminPage();
		String rows = (String) request.getParameter("rows"); // 每页条数
		query.setSize(Integer.valueOf(rows));
		Pageable pageKit = new PageKit(query.getPage(), query.getSize(),
				new PageOrder(Direction.DESC, "id"));
		Page<SendDeliverySysParamVo> page = sendDeliverySysParamService.queryPageList(
				query, pageKit);
		adminPage.setRows(page.getContent());
		adminPage.setTotal(Integer.valueOf(page.getTotalElements() + ""));
		return adminPage;
	}
	
	@RequestMapping(value = "/edit/{id}", method = RequestMethod.GET)
	public String edit(@PathVariable Integer id, Model model) {
		SendDeliverySysParamVo sendDeliverySysParamVo = sendDeliverySysParamService.getById(id);
		model.addAttribute("entity", sendDeliverySysParamVo);
		return "/send/deliverySysParam-edit";
	}
	
	@RequestMapping(value = "/edit", method = RequestMethod.POST)
	@ResponseBody
	public Result doEdit(SendDeliverySysParamVo sendDeliverySysParamVo) {
		Result ret = new Result();
		if (sendDeliverySysParamVo == null || sendDeliverySysParamVo.getId() == null) {
			ret.addErr("参数不能为空");
		} else {
			if (StringUtil.isBlank(sendDeliverySysParamVo.getKey())) {
				ret.addErr("key不能为空");
			}
			if (StringUtil.isBlank(sendDeliverySysParamVo.getValue())) {
				ret.addErr("value不能为空");
			}
			if (StringUtil.isBlank(sendDeliverySysParamVo.getRemark())) {
				ret.addErr("备注不能为空");
			}
		}
		if (ret.checkForm()) {
			SendDeliverySysParam update = new SendDeliverySysParam();
			update.setId(sendDeliverySysParamVo.getId());
			update.setKey(sendDeliverySysParamVo.getKey().trim());
			update.setValue(sendDeliverySysParamVo.getValue().trim());
			update.setRemark(sendDeliverySysParamVo.getRemark());
			update.setUpdAt(TimeUtil.now());
			sendDeliverySysParamService.updateById(update);
		}
		return ret;
	}
}
